package com.pxene.cjh.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 读取配置文件属性工具类
 * 
 * @author dajiangtai created by 2016-10-29
 */
public class LoadPropertyUtil {
	private static Properties properties;
	static{
		 properties = new Properties();
		InputStream inputStream = LoadPropertyUtil.class.getClassLoader().getResourceAsStream("task.properties");
		try {
		properties.load(inputStream);
		} catch (IOException e) {
		e.printStackTrace();
		}
	}

	// 读取优酷配置文件
	public static String getYOUKU(String key) {
		String value = "";
		Locale locale = Locale.getDefault();
		try {
			ResourceBundle localResource = ResourceBundle.getBundle("youku",
					locale);
			value = localResource.getString(key);
		} catch (MissingResourceException mre) {
			value = "";
		}
		return value;
	}

	// 读取公共配置文件
	public static String getConfig(String key) {
		String value = "";
		Locale locale = Locale.getDefault();
		try {
			ResourceBundle localResource = ResourceBundle.getBundle("config",
					locale);
			value = localResource.getString(key);
		} catch (MissingResourceException mre) {
			value = "";
		}
		return value;
	}
	
	   /**
     * 获得给定Properties类型引用所对应的资源文件中的所有键值对。
     * @param properties Properties类型的引用
     * @return 资源文件中的所有键值对
     */
    public static Map<String, String> getPropertyMap() {
        Map<String, String> propertyMap = new HashMap<String, String>();
         
        for (Iterator<Object> iter = properties.keySet().iterator(); iter.hasNext();) {
            String key = iter.next().toString();
            propertyMap.put(key, properties.getProperty(key));
        }
        return propertyMap;
    }
    
    
    

	public static void main(String[] args) {
		System.out.println(getPropertyMap().size());
	}
}
